package DAO;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.log4j.Logger;

import denisLunchy.*;

public class DBdishDAO implements ADao {
	String user;
	String password;
	String url;
	String driver;
	Connection c = null;
	Properties dbProp = new Properties();
	DataInputStream is = null;
	Logger log;

	public DBdishDAO() {
		// TODO Auto-generated constructor stub
		log = Logger.getLogger("simple");
		try {
			is = new DataInputStream(new FileInputStream("DBprop.txt"));
			dbProp.load(is);
			is.close();
		} catch (FileNotFoundException e2) {
			// TODO Auto-generated catch block
			log.error("Error", e2);
			e2.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			log.error("Error", e1);
			e1.printStackTrace();
		}
		user = dbProp.getProperty("user");
		password = dbProp.getProperty("pass");
		url = dbProp.getProperty("url");
		driver = dbProp.getProperty("driver");
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			log.error("Error", e);
			e.printStackTrace();

		}
	}

	@Override
	public void save(Object object) {
		// TODO Auto-generated method stub
		Dish dish = (Dish) object;
		String name = dish.getName();
		String type = dish.getType();
		int cost = dish.getCost();
		int count = dish.getCount();
		boolean rmv = dish.getStatus();
		c = null;
		try {
			c = DriverManager.getConnection(url, user, password);
			Statement st = c.createStatement();
			st.executeUpdate("INSERT INTO menu (name1, cost1, count1,removed,typeDish) VALUES('"
					+ name
					+ "'"
					+ ","
					+ cost
					+ ","
					+ count
					+ ","
					+ rmv
					+ ",'"
					+ type + "')");

		} catch (Exception e) {
			log.error("Error", e);
			e.printStackTrace();
		} finally {
			try {
				if (c != null)
					c.close();
			} catch (SQLException e) {
				log.error("Error", e);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public LunchyClass getObject(int id) {
		// TODO Auto-generated method stub
		Dish dish = new Dish();
		c = null;
		try {
			c = DriverManager.getConnection(url, user, password);
			Statement st = c.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM menu WHERE id=" + id);
			while (rs.next()) {
				dish.setType(rs.getString(5));
				dish.setId(Integer.parseInt(rs.getString(1)));
				dish.setName(rs.getString(2));
				dish.setCost(Integer.parseInt(rs.getString(3)));
				dish.setCount(Integer.parseInt(rs.getString(4)));
			}
		} catch (Exception e) {
			log.error("Error", e);
			e.printStackTrace();
		} finally {
			try {
				if (c != null)
					c.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				log.error("Error", e);
				e.printStackTrace();
			}
		}
		return dish;
	}

	@Override
	public void update(Object object) {
		// TODO Auto-generated method stub
		Dish dish = (Dish) object;
		int id = dish.setId();
		c = null;
		try {
			c = DriverManager.getConnection(url, user, password);
			Statement st = c.createStatement();
			st.executeUpdate(" update menu set id=" + id + ", name1='"
					+ dish.getName() + "', cost1=" + dish.getCost()
					+ ",count1=" + dish.getCount() + ",removed="
					+ dish.getStatus() + " where id =" + id);
		} catch (Exception e) {
			log.error("Error", e);
			e.printStackTrace();
		} finally {
			try {
				if (c != null)
					c.close();
			} catch (SQLException e) {
				log.error("Error", e);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void remove(int id) {
		// TODO Auto-generated method stub
		c = null;
		try {
			c = DriverManager.getConnection(url, user, password);
			Statement st = c.createStatement();
			st.executeUpdate("DELETE FROM menu WHERE id=" + id);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (c != null)
					c.close();
			} catch (SQLException e) {
				log.error("Error", e);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public ArrayList<Object> getAll() {
		// TODO Auto-generated method stub
		ArrayList<Object> Menu = new ArrayList<Object>();
		Dish dish = new Dish();
		c = null;
		try {
			c = DriverManager.getConnection(url, user, password);
			Statement st = c.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM menu");
			while (rs.next()) {
				dish = new Dish();
				dish.setStatus(rs.getBoolean(5));
				dish.setType(rs.getString(6));
				dish.setId(Integer.parseInt(rs.getString(1)));
				dish.setName(rs.getString(2));
				dish.setCost(Integer.parseInt(rs.getString(3)));
				dish.setCount(Integer.parseInt(rs.getString(4)));
				Menu.add(dish);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (c != null)
					c.close();
			} catch (SQLException e) {
				log.error("Error", e);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return Menu;
	}

	public void clear() {
		c = null;
		try {
			c = DriverManager.getConnection(url, user, password);
			Statement st = c.createStatement();
			st.executeUpdate("DELETE FROM menu");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (c != null)
					c.close();
			} catch (SQLException e) {
				log.error("Error", e);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
